<?php

// ==============================================================================
//
// This file is part of the WelSpeak.
//
// Create by Welfony Support <support@welfony.com>
// Copyright (c) 2012-2014 welfony.com
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
// ==============================================================================

namespace Welfony\Welspeak\Repository;

class UserRepository
{

    public static function getAllUsersCount()
    {
        global $app;

        $strSql = "SELECT
                       COUNT(1) `Total`
                   FROM Users
                   LIMIT 1";

        $row = $app->conn->fetchAssoc($strSql);

        return $row['Total'];
    }

    public static function getAllUsers($page, $pageSize)
    {
        global $app;

        $offset = ($page - 1) * $pageSize;

        $strSql = "SELECT
                       U.UserId,
                       U.PIN,
                       U.Username,
                       U.Email,
                       U.Gender,
                       U.Birthday,
                       U.AvatarUrl,
                       U.CreatedDate
                   FROM Users U
                   ORDER BY U.UserId DESC
                   LIMIT $offset, $pageSize";

        return $app->conn->fetchAll($strSql);
    }

    public static function findUserById($id)
    {
        global $app;

        $strSql = 'SELECT
                       US.UserId,
                       U.PIN,
                       US.Username,
                       US.Email,
                       US.AvatarUrl,
                       US.ProfileBackgroundUrl,
                       US.SocialExternalId,
                       US.SocialType
                   FROM vwUserSocial US
                   WHERE US.UserId = ?
                   LIMIT 1';

        return $app->conn->fetchAssoc($strSql, array($id));
    }

    public static function findUserByUsernameOrEmail($usernameOrEmail)
    {
        global $app;

        $strSql = 'SELECT
                       U.UserId,
                       U.PIN,
                       U.Username,
                       U.Email,
                       U.Gender,
                       U.Birthday,
                       U.AvatarUrl,
                       U.Password,
                       U.ForgotPasswordEmailToken,
                       U.ForgotPasswordEmailCreateDate,
                       U.CreatedDate,
                       U.LastModifiedDate
                   FROM Users U
                   WHERE U.Username = ? OR U.Email = ?
                   LIMIT 1';

        return $app->conn->fetchAssoc($strSql, array($usernameOrEmail, $usernameOrEmail));
    }

    public static function findUserBySocialExternalId($socialExternalId, $socialType)
    {
        global $app;

        $strSql = 'SELECT
                       U.UserId,
                       U.PIN,
                       U.Username,
                       U.Email,
                       U.Gender,
                       U.Birthday,
                       U.AvatarUrl,
                       U.CreatedDate,
                       U.LastModifiedDate
                   FROM Users U
                   INNER JOIN Social S ON S.UserId = U.UserId
                   WHERE S.ExternalId = ? AND S.Type = ?
                   LIMIT 1';

        $stmt = $app->conn->prepare($strSql);
        $stmt->bindValue(1, $socialExternalId);
        $stmt->bindValue(2, $socialType);
        $stmt->execute();

        return $stmt->fetch();
    }

    public static function save($data)
    {
        global $app;

        try {
            if ($app->conn->insert('Users', $data)) {
                return $app->conn->lastInsertId();
            }
        } catch (\Exception $e) {
            $app->log->getWriter()->write($e, \Slim\Log::ERROR);
            return false;
        }

        return false;
    }

    public static function update($userId, $data)
    {
        global $app;

        try {
            return $app->conn->update('Users', $data, array('UserId' => $userId));
        } catch (\Exception $e) {
            $app->log->getWriter()->write($e, \Slim\Log::ERROR);
            return false;
        }
    }

}